import { findAllCategory } from '@/api/category'
import { topCategory } from '@/api/constants'
export default {
  namespaced: true,
  state: {
    // 存放所有的分类数据
    list: topCategory.map(item => ({ name: item }))
  },
  mutations: {
    setList(state, list) {
      state.list = list
    },
    show(state, id) {
      const category = state.list.find(item => item.id === id)
      category.open = true
    },
    hide(state, id) {
      const category = state.list.find(item => item.id === id)
      category.open = false
    }
  },
  actions: {
    async getList({ commit }) {
      const res = await findAllCategory()
      res.result.forEach(item => {
        // 添加一个open属性，用于控制二级分类的显示和隐藏
        item.open = false
      })
      commit('setList', res.result)
    }
  }
}
